package cn.com.b2office.dao.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.com.b2office.dao.ItemCollectionDao;
import cn.com.b2office.domain.Item;
import cn.com.b2office.domain.ItemCollection;

public class ItemCollectionDaoHibernateImpl extends HibernateDaoSupport
		implements ItemCollectionDao {

	private static final Log logger = LogFactory
			.getLog(ItemCollectionDaoHibernateImpl.class);

	public void add(ItemCollection coll) {
		Session s = getSession();
		try {
			int count = ((Long) s.createQuery(
					"select count(*) from ItemCollection where memberId='"
							+ coll.getMemberId() + "' and itemId='"
							+ coll.getItemId() + "'").iterate().next())
					.intValue();
			// 如果不重复则增加
			if (count == 0) {
				Item item = (Item) s.get(Item.class, coll.getItemId());
				if (item != null) {
					coll.setCategoryId(item.getItem_clsno());
					s.save(coll);
				}
			}
		} catch (RuntimeException e) {
			logger.error("add(ItemCollection coll) throw Exception: "
					+ e.getMessage());
			throw e;
		} finally {
			releaseSession(s);
		}
	}

	@SuppressWarnings("unchecked")
	public List<ItemCollection> listCollection(String memberId,
			int firstResult, int maxResults) {
		List<ItemCollection> list = null;
		Session s = getSession();
		try {
			list = s
					.createQuery("from ItemCollection where memberId=:memberId")
					.setString("memberId", memberId)
					.setFirstResult(firstResult).setMaxResults(maxResults)
					.list();
		} catch (RuntimeException e) {
			logger.error("listCollection throw Exception: " + e.getMessage());
			throw e;
		} finally {
			releaseSession(s);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<ItemCollection> listCollection(String memberId,
			String parentCategoryId, int firstResult, int maxResults) {
		List<ItemCollection> list = null;
		Session s = getSession();
		try {
			list = s
					.createQuery(
							"from ItemCollection where memberId=:memberId and subString(categoryId,1,2)=:categoryId")
					.setString("memberId", memberId).setString("categoryId",
							parentCategoryId).setFirstResult(firstResult)
					.setMaxResults(maxResults).list();
		} catch (RuntimeException e) {
			logger.error("listCollection throw Exception: " + e.getMessage());
			throw e;
		} finally {
			releaseSession(s);
		}
		return list;
	}

	public void remove(int idno) {
		Session s = getSession();
		try {
			ItemCollection coll = (ItemCollection) s.load(ItemCollection.class,
					idno);
			s.delete(coll);
			s.flush();
		} catch (RuntimeException e) {
			logger.error("remove(int idno) throw Exception: " + e.getMessage());
			throw e;
		} finally {
			releaseSession(s);
		}
	}

	public int getTotalCollectionSize(String memberId) {
		int size = 0;
		Session s = getSession();
		try {
			Query query = s
					.createQuery(
							"select count(*) from ItemCollection where memberId=:memberId")
					.setString("memberId", memberId);
			size = ((Long) query.iterate().next()).intValue();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			releaseSession(s);
		}
		return size;
	}

	public int getTotalCollectionSize(String memberId, String categoryId) {
		int size = 0;
		Session s = getSession();
		try {
			Query query = s
					.createQuery(
							"select count(*) from ItemCollection where memberId=:memberId and subString(categoryId,1,2)=:categoryId")
					.setString("memberId", memberId).setString("categoryId",
							categoryId);
			size = ((Long) query.iterate().next()).intValue();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			releaseSession(s);
		}
		return size;
	}

	public List<ItemCollection> listAllCollection(String memberId) {
		return getHibernateTemplate().find(
				"from ItemCollection where memberId=?", memberId);
	}

}
